

data {
  int<lower=1> N; // Number of observation. (for parties)
  int<lower=1> I; // Number of items (same for parties and cantons)
  int<lower=1> J; // Number of Actors
  int<lower=1> T; // Number of time point
  int<lower=0> y[N]; // list of outcome (binary for parties)
  int<lower=1> i[N]; // list of items 
  int<lower=1> j[N]; // list of actors
  int<lower=1> t[N]; // list of time points
  //real betas_vals[I];
}


parameters {
  vector[I] alpha; // Difficulty parameter 
  vector[I] beta;  // Discrimination parameter
  vector[J] theta;  // ideal position of actor j at time t;
  vector[J] time[T-1]; // random time variable.
  vector<lower=0, upper =1>[J] time_var; // Time variance
}

transformed parameters {
  vector[J] theta2[T];
  vector[N] p_binary; 
  for(n in 1:T) {
    if(n==1) {
      theta2[n] = theta; // At time = 0
    } if(n>1) {
      theta2[n] = theta2[n-1] + time[n-1] .* time_var; // Otherwise normally distributed around the observation at previous time
    }
  }
  
  for (n in 1:N) {
    p_binary[n] = theta2[t[n], j[n]] * beta[i[n]] + alpha[i[n]];
  }
  
}


model {
  
  // for (n in 1:I) {
  //   beta[n] ~ normal(betas_vals[n], 1);
  // }
  
  
  alpha ~ normal(0, 5);
  beta ~ normal(0, 5);
  target += exponential_lpdf(time_var| 20); // Prior for the time 
  theta ~ std_normal();
  
  // at time 1, no difference between theta and theta2
  
  for (n in 1:T-1) {
    time[n] ~ std_normal();
  }
  y ~ bernoulli_logit(p_binary);
}







